package com.dhgate.spu.analysis.util;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author: wangguodong
 * @create: 2014-3-10 上午11:49:00
 * @description:
 */
public class CommonUtil {
	private static final Logger logger = LoggerFactory.getLogger(CommonUtil.class);
	private static final Analyzer ANALYZER = new SpuAnalyzer();

	public static void main(String[] args) throws Exception {
		String productNameArray[] = { "THE NEW NOTE MTK6589 Quad Core", "NEW NOTE MTK6572W Quad Core" };
		for (String pName : productNameArray) {
			StringReader reader = new StringReader(pName);
			TokenStream ts = ANALYZER.tokenStream(null, reader);
			CharTermAttribute term = ts.getAttribute(CharTermAttribute.class);
			ts.reset();
			while (ts.incrementToken()) {
				System.out.print(term.toString() + " ");
			}
			ts.close();
			System.out.println();
		}
	}

	public static Set<String> getKeywordSet(String str) {
		Set<String> keywordSet = new HashSet<String>();
		TokenStream ts;
		try {
			ts = ANALYZER.tokenStream(null, new StringReader(str));
			CharTermAttribute term = ts.getAttribute(CharTermAttribute.class);
			ts.reset();
			while (ts.incrementToken()) {
				keywordSet.add(term.toString());
			}
			ts.close();
		} catch (IOException e) {
			logger.error(e + "");
			e.printStackTrace();
		}
		return keywordSet;
	}

	public static List<String> getUnionList(List<String> l1, List<String> l2) {
		List<String> unionList = new ArrayList<String>();
		Set<String> map = new HashSet<String>();

		for (String o : l1) {
			unionList.add(o);
			map.add(o);
		}

		for (String o : l2) {
			if (map.contains(o)) {
				continue;
			}
			unionList.add(o);
		}

		return unionList;
	}

	public static List<String> getInterList(List<String> l1, List<String> l2) {
		List<String> interList = new ArrayList<String>();
		Set<String> map = new HashSet<String>();

		for (String o : l1) {
			map.add(o);
		}

		for (String o : l2) {
			if (map.contains(o)) {
				interList.add(o);
			}
		}

		return interList;
	}

}
